package com.gochic.mid;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.gochic.DataUtil;
import com.gochic.MidObject;
import com.gochic.data.UserEntity;

public class User extends MidObject {
	private static final long serialVersionUID = 3184082675315486531L;
	public User() {
		setEntity( new UserEntity() );
	}
	public User(UserEntity ce) {
		super(ce);
	}
	public User save() {
        return new User( (UserEntity)saveEntity() );
	}
	public User getUserById( int id ) {
		return new User( (UserEntity) getEntityById( id ) );
	}

	public static List<User> getAllUsers() {
		PersistenceManager pm = DataUtil.getPersistenceManager();
		Query query = pm.newQuery(UserEntity.class);
		//query.setFilter( "status == " + Constant.Status.Active );
		//query.setOrdering( "editTime" );
		
		List<User> result = new ArrayList<User>(0);
		try {
			@SuppressWarnings("unchecked")
			List<UserEntity> ues = (List<UserEntity>) query.execute();
			for( UserEntity ue : ues ) {
				result.add( new User( ue ) );
			}
		} finally {
			pm.close();
		}
	    return result;
	}
	
	public String getName() {
		return ((UserEntity)this.getEntity()).getName();
	}
	public void setName(String name) {
		((UserEntity)this.getEntity()).setName(name);
	}
	
	public String getLevel() {
		return ((UserEntity)this.getEntity()).getLevel();
	}
	public void setLevel(String level) {
		((UserEntity)this.getEntity()).setLevel(level);
	}
}
